package activitys;

import io.web.JSONConnector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import misc.SpecialAdapter;
import nl.preflag.basewars.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

/**
 * @author Patrick van Gils, 0787862, INF3M
 * Friend activity shows all your friends
 */

public class FriendListActivity extends Activity {
	
	public ImageButton friendMapButton, friendActionButton, friendSearchButton;
	private JSONConnector webby;
	private JSONArray friendListJson, playerListJson;
	public HashMap<String, String> map, o;
	private String USER_ID, temp, temp1;

	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.friend_list);
        
        SharedPreferences pref = getSharedPreferences("programSettings", MODE_PRIVATE);
        USER_ID = pref.getString("userId", null);
        
        setView();
        fillFriendList();
    }
	
	/**
	 * Set the view
	 */
	private void setView() {
		friendMapButton = (ImageButton) findViewById(R.id.friendMapFriendButton);
		friendSearchButton = (ImageButton) findViewById(R.id.friendSearchButton);
		
		friendMapButton.setOnClickListener(new View.OnClickListener() {
	          public void onClick(View v) {
	        	  openMapView();
	          }
		});
		
		friendSearchButton.setOnClickListener(new View.OnClickListener() {
	          public void onClick(View v) {
	        	  openPlayerPrompt();
	          }
		});
	}
	
	/**
	 * Open the map activity
	 */
	private void openMapView() {
		finish();
	}
	
	/**
     * Fill the listview with base info
     */
	private void fillFriendList() {
		
		final ListView lv = (ListView) findViewById(R.id.friendListListView);
		String[] from = new String[] {"player_id", "friend_id"};
		int[] to = new int[] { R.id.friendListUsernameTextView, R.id.friendListEmailTextView};
		
		List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    	try 
    	{
    		webby = new JSONConnector(); 
    		friendListJson = webby.connect("/PlayerFriend/!%60player_id%60%20LIKE%20'"+USER_ID+"'", "result", false);

    		for(int i=0;i<friendListJson.length();i++) 
    		{
    			map = new HashMap<String, String>();	
    			JSONObject e = friendListJson.getJSONObject(i);
    			map.put("id",  ""+e.getInt("id"));
    			
    			try 
		    	{
    				playerListJson = webby.connect("/User/"+e.getInt("friend_id")+"", "result", true);
        			for (int j=0; j<playerListJson.length();j++) {
        				JSONObject f = playerListJson.getJSONObject(j);
        				temp = f.getString("username");
        				temp1 = f.getString("email");
        			}
		    	} catch (Exception ei) {
		    		temp = "Geen username";
		    		temp1 = "Geen email adres";
		    	}
    			map.put("player_id", temp1);
    			map.put("friend_id", temp);
    			fillMaps.add(map);	
    		}
	     	
	   	} catch (Exception e) 
	   	{
	   		e.printStackTrace();
	   	}
    	// fill in the grid_item layout
        SpecialAdapter adapter = new SpecialAdapter(this, fillMaps, R.layout.friend_list_item, from, to);
        lv.setAdapter(adapter);
	}
	
	/**
	 * Search player dialog
	 */
	private void openPlayerPrompt() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("Search player");
		alert.setMessage("Fill in a username or a email address");
		final EditText input = new EditText(this);
		alert.setView(input);

		alert.setPositiveButton("Search", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
					String value = input.getText().toString();
						
					Intent mmIntent = new Intent(FriendListActivity.this, SearchListActivity.class);
					mmIntent.putExtra("query", value);
					startActivityForResult(mmIntent, 0);
					finish();
			}
		});
		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			  public void onClick(DialogInterface dialog, int whichButton) {
				  // Canceled.
			  }
		});
		alert.show();
	}
}
